const express=require('express');
const db = require('../utils/database');
const myRouter = express.Router();
// 获取
myRouter.get('/waste/get',(req,res)=>{
    const page=req.query.page;
    const count=req.query.count;
    const sql=`SELECT * FROM waste JOIN partner ON waste.w_partner_id=partner.p_id ORDER BY w_id LIMIT ${(page-1)*count},${count};`;
    const sql2 = `select count(*) as total from waste JOIN partner ON waste.w_partner_id=partner.p_id WHERE w_date LIKE '%${req.query.w_date}%' and w_species like '%${req.query.w_species}%' AND p_name LIKE '%${req.query.w_parnter}%'`;
    const sql3 = `SELECT * FROM waste JOIN partner ON waste.w_partner_id=partner.p_id WHERE w_date LIKE '%${req.query.w_date}%' and w_species like '%${req.query.w_species}%' AND p_name LIKE '%${req.query.w_parnter}%' ORDER BY w_id LIMIT ${(page - 1) * count},${count}`;
   
    db.query(sql,(err, data) => {
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            db.query(sql2,(err2,data2)=>{
                if(err2){
                    console.log(err2)
                    res.send({error:1,msg:err2})
                }else{
                   db.query(sql3,(err3,data3)=>{
                if(err3){
                    console.log(err3)
                    res.send({error:1,msg:err3})
                }else{
                    // console.log(data2[0].total)
                    res.send({error:0,msg:'查询成功',data:data,total:data2[0].total,searchDate:data3})
                }
            })
                }
            })
        }
    })
})


// 修改
myRouter.get('/waste/edit', (req, res) => {

    const sql = `UPDATE waste SET w_date='${req.query.w_date}',w_species='${req.query.w_species}',w_weight='${req.query.w_weight}',w_partner_id='${req.query.w_partner_id}' where w_id=${req.query.id} ORDER BY w_id desc;`;
  
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        } else {
           
            res.send({error:0,msg:'修改成功',data:data})
        }
    })
})
// 新增
myRouter.post('/waste/Add',(req,res)=>{
   
const sql =`INSERT INTO waste VALUES(NULL,'${req.body.w_date}','${req.body.w_species}','${req.body.w_weight}','${req.body.w_partner_id}');`
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            res.send({error:0,msg:'添加成功',data:data})
        }
    })
})











module.exports=myRouter;